/**
 * Created by Luji on 2017/4/21.
 */
import editTemp from './form/editForm.html';
import editCtrl from './form/editForm.ctrl.js';
import shareTemp from './share/share.html';
import shareCtrl from './share/share.ctrl.js';
class Image {
    constructor(imageSrv,list,userSrv, ctgColor,ctgNotification, $uiModal,$state,$scope,$rootScope) {
        "ngInject";
        this._imageSrv = imageSrv;
        this._$scope= $scope;
        this._userSrv=userSrv;
        this._notification = ctgNotification;
        this._color = ctgColor;
        this._uiModal = $uiModal;
        this._state=$state;
        this._root=$scope.$root;
        this.user=$scope.user;
        this.user={
           userCode:$rootScope.user.sysUserCode
        };
        this.page = {
            currentPage: 1,
            totalItems: 0,
            page:5
        };
    
        this.imageTypeList = [{ name: "基础镜像", type: '001' }, { name: "构建镜像", type: '002' }, { name: "与我分享", type: '003' }];
        this.filter = {
            imageType: this._root.imageType?this._root.imageType:'001',
            pageNum:0,
            pageSize:5
        }
        if (list) {
            this.table = list.list;
            angular.forEach(this.table, (images,ind) => {
                 images.border = { 'border-left': '2px groove ' + this._color[ind % this._color.length] };
                 images.color = { 'color': this._color[ind % this._color.length] };
            });
            this.page.totalItems = list.total;
        } else{
            this.table = [];
        }

    }
    setColor(images,ind){

                images.border = { 'border-left': '2px groove ' + this._color[ind % this._color.length] };
                images.color = { 'color': this._color[ind % this._color.length] };
    }
    setImageType(type) {
        this.filter.imageType =this._root.imageType= type;
        this.getList();
        this._$scope.searchBtn=false;
        if(type=='002'){
          this._imageSrv.isAdmin().then(data=>{           
        if(data==true){                      
              this._$scope.searchBtn=true;
              this._userSrv.getUserList({currentPage: this.page.currentPage}).then(data=>{
                // console.log(data);
                this.username=data.list;
              })
            }else{
              this._$scope.searchBtn=false;  
            } 
          }).catch((data)=>{
          this._ctgNotification.error('获取用户数据有误.');
         })
        }
        }   
    getList(obj) {
        this.table = null;
        this._imageSrv.getHarborImages({ currentPage: this.page.currentPage,page:this.page.page, imageType: this.filter.imageType, sysUserCode: this.user.userCode }).then(data => {
            this.table = data.list;
            console.log(this.table)
            this.page.totalItems = data.total;
            angular.forEach(data.list, (img,ind) => {
                this.setColor(img,ind);
                img.versions=null;
                this._imageSrv.getHarborVersions(img.imageName, this.filter.imageType).then(data => {
                    img.versions = data;
                }).catch(function(data) {
                    ctgNotification.error('获取我的镜像版本数据有误.');
                });
            })
        }).catch(() => {
            this.table = [];
            this.page.totalItems = 0;
            this._notification.error('获取Docker镜像列表数据有误.');
        });
    }
    editImage(version) {
        this._uiModal.open({
            templateUrl: editTemp,
            controller: editCtrl,
            controllerAs: 'vm',
            size: 'md',
            resolve: {
                version: () => {
                    return version
                }
            }
        }).result.then((data) => {
            this.getList();
        });
    }
    setConfigs(version) {
        this._root.MyimageName=version.imageName+':'+version.tag;
        this._state.go('image-config',{id:version.id});
    }
    setShare(version){
        console.log(version)
        this._uiModal.open({
            templateUrl:shareTemp,
            controller:shareCtrl,
            controllerAs:'vm',
            size:'lg',

            resolve:{
                tabId:()=>version.id,
                userCodeOfShare:()=>version.userCodeOfShare
            }
        }).result.then((data)=>{
            this._imageSrv.shareImage({sysUserIds:data,dockerImageTagId:version.id}).then(()=>{
                this._notification.success('共享成功');
            }).catch(()=>{
                this._notification.success('共享失败');
            });
        })
    }

    selectUser(){
    //    console.log(this.user)
       this.getList();
    }
}
export default Image;
